//
//  coocslessViewController.m
//  coocsless
//
//  Created by fabian on 02.03.11.
//  Copyright 2011 roboboogie. All rights reserved.
//

#import "ContainerViewController.h"
#import "GalleryViewController.h"
#import "Global.h"
#import "MovieViewController.h"

#define THUMB_HEIGHT 140
#define THUMB_WIDTH 250
#define THUMB_V_PADDING 4
#define THUMB_H_PADDING 10

@implementation ContainerViewController
@synthesize decadesPopover;
@synthesize categoriesPopover;
@synthesize uiPopoverController1;
@synthesize uiPopoverController2;
@synthesize movieViewController;



/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

-(void) viewWillAppear:(BOOL)animated {
	
}


// Implement loadView to create a view hierarchy programmatically, without using a nib.
-(void)loadView {
	[super loadView];

	
	movieViewController = [[MovieViewController alloc] initWithMovieName:@"35mm-707-neu_fc1_miro-ipad" 
											  type:@"mp4"
											  rect:CGRectMake(0,0, 1024,768) ];
	
	[self.navigationController pushViewController:movieViewController animated:NO];
	
	[movieViewController play]; 
	[self loadArticle];


}



- (void)loadArticle {
	//[super loadView];
	
	
	
	CGRect frame = CGRectMake(0, 0, 1024, 768);
	articleView = [[ArticleView alloc] initWithFrame:frame];
	[articleView setBackgroundColor:[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg2.png"]]];	
	
	[[self view] addSubview:articleView];
	
	
	///////// ZURÜCKBUTTON und GALLERYBUTTON mit homebuttonView Delegate
	
	frame = CGRectMake(100, 100, 240, 20);
	HomeButtonView *button = [[HomeButtonView alloc] initWithFrame:frame andTitle:@"home" andButtonFunction:@"home"];
	button.titleLabel.font = [UIFont fontWithName:@"Archer" size:20];
	///WICHTIG sich selbst als delegate melden 
	[button setDelegate:self];
	[[self view] addSubview:button];
	[button release];
	
	frame = CGRectMake(100, 140, 240, 20);
	HomeButtonView *galleryButton = [[HomeButtonView alloc] initWithFrame:frame andTitle:@"Bildergalerie" andButtonFunction:@"gotoGallery"];
	galleryButton.titleLabel.font = [UIFont fontWithName:@"Archer" size:20];

	///WICHTIG sich selbst als delegate melden 
	[galleryButton setDelegate:self];
	[[self view] addSubview:galleryButton];
	[galleryButton release];
	
	/////////
	
	
	frame = CGRectMake(0, 580, 1024, 190);
	scrollContainer = [[UIView alloc] initWithFrame:frame];
	[scrollContainer setOpaque:NO];
	[scrollContainer setBackgroundColor:[UIColor grayColor]];
	
	
	[[self view] addSubview:scrollContainer];
	
	////// Buttons für ScrollContainer
	
	UIImage *navBarBg = [UIImage imageNamed:@"navBarBg.png"];
	frame = CGRectMake(0, 0, 1005, 37);
	UIImageView *barView = [[UIImageView alloc] initWithImage:navBarBg];
	[scrollContainer addSubview:barView];
	[navBarBg release];
	[barView release];
	
	
	frame = CGRectMake(100, 11, 160, 20);
	HomeButtonView *decadesButton = [[HomeButtonView alloc] initWithFrame:frame andTitle:[[Global sharedGlobal]._decades objectAtIndex:[Global sharedGlobal].language] andButtonFunction:@"showDecadesMenu"];
	decadesButton.titleLabel.font = [UIFont fontWithName:@"Archer" size:20];
	[decadesButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[decadesButton setDelegate:self];
	[scrollContainer addSubview:decadesButton];
	[decadesButton release];
	
	frame = CGRectMake(270, 11, 160, 20);
	HomeButtonView *categoriesButton = [[HomeButtonView alloc] initWithFrame:frame andTitle:[[Global sharedGlobal]._categories objectAtIndex:[Global sharedGlobal].language] andButtonFunction:@"showCategoryMenu"];
	categoriesButton.titleLabel.font = [UIFont fontWithName:@"Archer" size:20];
	[categoriesButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[categoriesButton setDelegate:self];
	[scrollContainer addSubview:categoriesButton];
	[categoriesButton release];
	
	
	frame = CGRectMake(860, 11, 40, 20);
	HomeButtonView *searchButton = [[HomeButtonView alloc] initWithFrame:frame andTitle:@"SU" andButtonFunction:@"openSearchView"];
	searchButton.titleLabel.font = [UIFont fontWithName:@"Archer" size:20];
	[searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[searchButton setDelegate:self];
	[scrollContainer addSubview:searchButton];
	[searchButton release];
	
	frame = CGRectMake(900, 11, 40, 20);
	HomeButtonView *germanButton = [[HomeButtonView alloc] initWithFrame:frame andTitle:@"DE" andButtonFunction:@"changeLanguageTo0"];
	germanButton.titleLabel.font = [UIFont fontWithName:@"Archer" size:20];
	[germanButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[germanButton setDelegate:self];
	[scrollContainer addSubview:germanButton];
	[germanButton release];
	
	frame = CGRectMake(940, 11, 40, 20);
	HomeButtonView *englishButton = [[HomeButtonView alloc] initWithFrame:frame andTitle:@"EN" andButtonFunction:@"changeLanguageTo1"];
	englishButton.titleLabel.font = [UIFont fontWithName:@"Archer" size:20];
	[englishButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[englishButton setDelegate:self];
	[scrollContainer addSubview:englishButton];
	[englishButton release];
	
	//////
	NSArray *imageNames = [[NSArray alloc] initWithObjects:@"thumb2.png", @"thumb3.png", @"thumb4.png",@"thumb2.png", @"thumb3.png", @"thumb4.png",nil];
	
	float scrollViewHeight = THUMB_HEIGHT + THUMB_V_PADDING;

	// merke width und height vom view sind vertauscht
	thumbScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, [[self view] bounds].size.height, scrollViewHeight)];
	[thumbScrollView setCanCancelContentTouches:NO];
	[thumbScrollView setClipsToBounds:NO];
	
	float xPosition = THUMB_H_PADDING;
	for (NSString *name in imageNames) {
		UIImage *thumbImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@", name]];
		if (thumbImage) {
			ThumbImageView *thumbView = [[ThumbImageView alloc] initWithImage:thumbImage];
			[thumbView setDelegate:self];
			[thumbView setImageName:name];
			CGRect frame = [thumbView frame];
			frame.origin.y = THUMB_V_PADDING;
			frame.origin.x = xPosition;
			[thumbView setFrame:frame];
			[thumbView setHome:frame];
			[thumbScrollView addSubview:thumbView];
			[thumbView release];
			xPosition += (frame.size.width + THUMB_H_PADDING);
		}
	}
	[thumbScrollView setContentSize:CGSizeMake(xPosition, scrollViewHeight)];
	[scrollContainer addSubview:thumbScrollView];
	
	
	////// Hier wird der Content für den Popover geladen, abhängig von der Sprache
	
	self.categoriesPopover = [[[PopoverView alloc] initWithStyle:UITableViewStylePlain andContent:@"categories" andWidth:200] autorelease];
	categoriesPopover.delegate = self;
	self.uiPopoverController2 = [[[UIPopoverController alloc] initWithContentViewController:self.categoriesPopover ] autorelease]; 
	

}

#pragma mark ThumbImageViewDelegate methods

- (void)thumbImageViewWasTapped:(ThumbImageView *)tiv {
    NSLog(@"%@",tiv.imageName);
	[[articleView label] setText:tiv.imageName];
	//[self.navigationController popViewControllerAnimated:NO];
}

#pragma mark HomeButtonViewDelegate methods

-(void) homeButtonViewWasTapped:(HomeButtonView *)hbv {
	if (hbv.buttonFunction==@"gotoGallery") {
		GalleryViewController *galleryViewController = [[GalleryViewController alloc] init];
		[self.navigationController pushViewController:galleryViewController animated:NO];
	
	} else if (hbv.buttonFunction==@"showDecadesMenu") {
		
		if (decadesPopover == nil) {
			self.decadesPopover = [[[PopoverView alloc] initWithStyle:UITableViewStylePlain andContent:@"decades" andWidth:150] autorelease];
			decadesPopover.delegate = self;
			self.uiPopoverController1 = [[[UIPopoverController alloc] initWithContentViewController:self.decadesPopover] autorelease];               
		}
		[self.uiPopoverController1 presentPopoverFromRect:CGRectMake(100, 0, 160, 250) inView:scrollContainer permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

	} else if (hbv.buttonFunction == @"showCategoryMenu") {
		
		[self.uiPopoverController2 presentPopoverFromRect:CGRectMake(240, 0, 210, 250) inView:scrollContainer permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];	
		
	} else if (hbv.buttonFunction==@"home") {
		[self.navigationController popViewControllerAnimated:NO];
	} else if (hbv.buttonFunction==@"changeLanguageTo0") {
		[Global sharedGlobal].language = 0;
		[self loadView]; 
	} else if (hbv.buttonFunction==@"changeLanguageTo1") {
		[Global sharedGlobal].language = 1;
		[self loadView]; 
	} else if (hbv.buttonFunction==@"openSearchView") {
		//[self.navigationController pushViewController:galleryViewController animated:NO];
	}
}

#pragma mark PopoverViewDelegate methods

-(void) itemSelected:(int)itemId {
	[Global sharedGlobal].currentPage = itemId;
	
	if(uiPopoverController1.popoverVisible == YES) [Global sharedGlobal].mode = 0; 
	if(uiPopoverController2.popoverVisible == YES) [Global sharedGlobal].mode = 1; 
	
	[uiPopoverController1 dismissPopoverAnimated:NO];
	[uiPopoverController2 dismissPopoverAnimated:NO];
	[self.navigationController popViewControllerAnimated:NO];
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}	


- (void)dealloc {	
	[decadesPopover release];
	[categoriesPopover release];
	[uiPopoverController1 release];
	[uiPopoverController2 release];
    [super dealloc];
}



@end
